from django.db import models

class VersionedContentType(models.Model):
    """
    """
    
    name = models.CharField(maxlength = 200)

    def __str__(self):
        return self.name
    
    class Admin:
        pass
    

class VersionedContentInfo(models.Model):
    """

    storage of all content ids referred to by other users of the
    versioned content store
    
    """

    current_version = models.PositiveIntegerField(default = 1)
    content_type = models.ForeignKey(VersionedContentType)

    def __str__(self):
        return str(self.current_version) + '-' + str(self.content_type)
    
    class Admin:
        pass

    class Meta:
        verbose_name_plural = 'Versioned content info'
        

    def get_version(self, version = -1):
        """

            Returns an instance of VersionedContentStore
            
        """

        if version == -1: # get the latest version
            version = self.current_version

        print self.versionedcontentstore_set
        
        return self.versionedcontentstore_set.all()[version - 1]


class VersionedContentStore(models.Model):
    """

    storage for all versioned content with just the identifier
    and the versioned content
    """
    
    content_info = models.ForeignKey(VersionedContentInfo)
    content = models.TextField()
    content_version = models.PositiveIntegerField(default = 1)

    def __str__(self):
        return str(self.content_info) + '_' + str(self.content_version)    

    class Meta:
        unique_together = (("content_info", "content_version"),)
        verbose_name_plural = 'Versioned content store'
        
    class Admin:
        pass

